/*
 * downloadmanager.h
 *
 *  Created on: 2010-3-23
 *      Author: alex
 */

#ifndef DOWNLOADMANAGER_H
#define DOWNLOADMANAGER_H

#include "cx_system_define.h"
#include "comm_objbase.h"
#include "comm_llist.h"

typedef enum
{
	LIST_TYPE_WORK,
	LIST_TYPE_DONE
}task_type_e;

typedef enum
{
	qqr_shl_status_idle,
	qqr_shl_status_disconnect,
	qqr_shl_status_connecting,
	qqr_shl_status_complete
}task_shl_status_e;

typedef struct
{
	unsigned long		ref_cnt;
	unsigned short*		root_path;	
	comm_llist_t		work_list_data;
	comm_llist_t		done_list_data;
	COMM_REF(ICommO)	observer;
	unsigned short*		download_path;	
}download_manager_t,*download_manager_ptr_t;

#define 	MAX_URL			512

typedef struct
{
	comm_llink_t		link;
	unsigned short		url[MAX_URL];
	unsigned short*		filename;
	unsigned long		task_id;
	unsigned long		res_id;
	unsigned long		total_bytes;
	unsigned long		recvd_bytes;
	unsigned int		status;
	unsigned short*		operator_id;
	COMM_REF(ICommO)	task;
}download_item_t,*download_item_ptr_t;

COMM_INTERFACE_( IDownloadStatusObserver, 1, ICommO )
{
	__COMM_INHERIT_FROM( ICommO )

	COMM_METHOD( NotifyGetLinkEvent)  ( COMM_REF(ICommO) thiz,  IN int status,IN unsigned short* filename );
	COMM_METHOD( NotifyEvent)			( COMM_REF(ICommO) thiz,  IN int status,IN unsigned short* filename );
	COMM_METHOD( NotifyStatus)			( COMM_REF(ICommO) thiz,  IN int status,IN unsigned short* filename );
	COMM_METHOD( NotifyProgress)		(COMM_REF(ICommO) thiz, IN int aReceived,IN unsigned short* filename);
	COMM_METHOD( NotifyTotalProgress)	(COMM_REF(ICommO) thiz, IN int aReceived, IN int aTotalSize,IN unsigned short* filename);
};

#define IComm_cx_DownloadStatusObserver_VTBL( p )		              		COMM_GET_VTBL_(p, IDownloadStatusObserver)
#define IComm_cx_DownloadStatusObserver_CALL( p, a )		            	IComm_cx_DownloadStatusObserver_VTBL( p )->a
#define IComm_cx_DownloadStatusObserver_NotifyGetLinkEvent( p, a ,b)       IComm_cx_DownloadStatusObserver_CALL( p, NotifyGetLinkEvent )( p, a ,b );
#define IComm_cx_DownloadStatusObserver_NotifyEvent( p, a ,b)			    IComm_cx_DownloadStatusObserver_CALL( p, NotifyEvent )( p, a ,b);
#define IComm_cx_DownloadStatusObserver_NotifyStatus( p, a, b)           	IComm_cx_DownloadStatusObserver_CALL( p, NotifyStatus )( p, a,b )
#define IComm_cx_DownloadStatusObserver_NotifyProgress( p, a,b)         	IComm_cx_DownloadStatusObserver_CALL( p, NotifyProgress )( p, a,b)
#define IComm_cx_DownloadStatusObserver_NotifyTotalProgress( p, a ,b,c)  	IComm_cx_DownloadStatusObserver_CALL( p, NotifyTotalProgress )( p, a ,b,c)

COMM_INTERFACE_( IDownloadManager, 1, IDownloadStatusObserver )
{
	__COMM_INHERIT_FROM( IDownloadStatusObserver )
	COMM_METHOD( AddTask)		       ( COMM_REF(ICommO) thiz, IN void* data );
	COMM_METHOD( AddOldTask)          ( COMM_REF(ICommO) thiz, IN void* data );
	COMM_METHOD( DealTaskOne)		   ( COMM_REF(ICommO) thiz, IN unsigned short* filename ,IN int deletefile);
	COMM_METHOD( SetObserver )		   ( COMM_REF(ICommO) thiz, IN COMM_REF(ICommO) obsvr );
	COMM_METHOD( Operate)             ( COMM_REF(ICommO) thiz, IN unsigned short* filename);
	COMM_METHOD( DeleteAll)           ( COMM_REF(ICommO) thiz);
	COMM_METHOD( StartAll)            ( COMM_REF(ICommO) thiz);
	COMM_METHOD( PauseAll)            ( COMM_REF(ICommO) thiz);
	COMM_METHOD( LaunchTask)          ( COMM_REF(ICommO) thiz, IN QBOOL islaunchtask);
	COMM_METHOD( Save)                ( COMM_REF(ICommO) thiz);
	COMM_METHOD( SetDownloadPath)          ( COMM_REF(ICommO) thiz, IN unsigned short* path);
	COMM_METHOD( SetIap)          ( COMM_REF(ICommO) thiz, IN int iap);
	COMM_METHOD( GetDownloadInfoList)          ( COMM_REF(ICommO) thiz, IN void* data);
};

/*************************************************************************
* DECLARATION
*************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif	// #if defined(__cplusplus)

COMM_IMPORT COMM_REF(ICommO)	comm_cx_create_download_manager( void * reserved );

#if defined(__cplusplus)
}
#endif	// #if defined(__cplusplus)

/*************************************************************************
* MACRO
*************************************************************************/


#define IDownloadManager_VTBL( p )				    COMM_GET_VTBL_(p, IDownloadManager)
#define IDownloadManager_CALL( p, a )				IDownloadManager_VTBL(p)->a
#define IDownloadManager_AddTask( p, a )			IDownloadManager_CALL( p, AddTask)( p, a )
#define IDownloadManager_AddOldTask( p, a )			IDownloadManager_CALL( p, AddOldTask)( p, a )
#define IDownloadManager_DelTaskOne( p, a ,b)		IDownloadManager_CALL( p, DealTaskOne )( p, a ,b)
#define IDownloadManager_SetObserver( p, a )		IDownloadManager_CALL( p, SetObserver )( p, a )
#define IDownloadManager_Operate( p, a )		    IDownloadManager_CALL( p, Operate )( p, a )
#define IDownloadManager_DeleteAll( p )		        IDownloadManager_CALL( p, DeleteAll )( p )
#define IDownloadManager_StartAll( p )		        IDownloadManager_CALL( p, StartAll )( p )
#define IDownloadManager_PauseAll( p )		        IDownloadManager_CALL( p, PauseAll )( p )
#define IDownloadManager_LaunchTask( p, a )		    IDownloadManager_CALL( p, LaunchTask )( p, a )
#define IDownloadManager_Save( p )		            IDownloadManager_CALL( p, Save )( p )
#define IDownloadManager_SetDownloadPath( p, a )			IDownloadManager_CALL( p, SetDownloadPath)( p, a )
#define IDownloadManager_SetIap( p, a )			IDownloadManager_CALL( p, SetIap)( p, a )
#define IDownloadManager_GetDownloadInfoList( p, a )			IDownloadManager_CALL( p, GetDownloadInfoList)( p, a )
#endif
